package com.acwing.partition11;

import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/12/21 16:15
 */
public class AC1069凸多边形的划分 {

    private static final int INF = (int) 1e9;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split("\\s+");
        int n = Integer.parseInt(s[0]);
        int[] w = new int[n];
        s = reader.readLine().split("\\s+");
        for (int i = 0; i < n; i++) w[i] = Integer.parseInt(s[i]);
        List<Integer> ans = dynamicProgramming2(w);
        for (int i = ans.size() - 1; i >= 0; i--) writer.write(String.valueOf(ans.get(i)));
        writer.flush();
    }

    private static List<Integer> dynamicProgramming2(int[] w) {
        int n = w.length;
        List<Integer>[][] dp = new ArrayList[n][n];
        //初始化dp数组
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                List<Integer> item = new ArrayList<>();
                item.add(0);
                dp[i][j] = item;
            }
        }
        for (int length = 3; length <= n; length++) {
            for (int i = 0; i + length - 1 < n; i++) {
                int j = i + length - 1;
                for (int k = i + 1; k < j; k++) {
                    //使用高精度加法和高精度乘法代替原状态转移方程式
                    List<Integer> cal = generateList(String.valueOf(w[i]));
                    cal = multiply(cal, w[k]);
                    cal = multiply(cal, w[j]);
                    cal = add(cal, dp[i][k]);
                    cal = add(cal, dp[k][j]);
                    if (dp[i][j].size() == 1 && dp[i][j].get(0) == 0) dp[i][j] = cal;
                    else if (compare(cal, dp[i][j]) < 0) dp[i][j] = cal;
                }
            }
        }
        return dp[0][n - 1];
    }

    private static List<Integer> generateList(String num) {
        List<Integer> res = new ArrayList<>(num.length());
        for (int i = num.length() - 1; i >= 0; i--) res.add(num.charAt(i) - '0');
        return res;
    }

    private static int compare(List<Integer> a, List<Integer> b) {
        if (a.size() != b.size()) return a.size() - b.size();
        for (int i = a.size() - 1; i >= 0; i--) {
            if (a.get(i).equals(b.get(i))) continue;
            return a.get(i) - b.get(i);
        }
        return 0;
    }

    private static List<Integer> multiply(List<Integer> a, long b) {
        List<Integer> answer = new ArrayList<>();
        long t = 0;
        for (int i = 0; i < a.size(); i++) {
            t += b * a.get(i);
            answer.add((int) (t % 10));
            t /= 10;
        }
        while (t != 0) {
            answer.add((int) (t % 10));
            t /= 10;
        }
        return answer;
    }

    private static List<Integer> add(List<Integer> a, List<Integer> b) {
        List<Integer> answer = new ArrayList<>();
        int t = 0;
        for (int i = 0; i < a.size() || i < b.size(); i++) {
            if (i < a.size()) t += a.get(i);
            if (i < b.size()) t += b.get(i);
            answer.add(t % 10);
            t /= 10;
        }
        while (t != 0) {
            answer.add(t % 10);
            t /= 10;
        }
        return answer;
    }

    private static String dynamicProgramming1(int[] w) {
        int n = w.length;
        BigInteger[][] dp = new BigInteger[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) dp[i][j] = BigInteger.ZERO;
        }
        for (int length = 3; length <= n; length++) {
            for (int i = 0; i + length - 1 < n; i++) {
                int j = i + length - 1;
                for (int k = i + 1; k < j; k++) {
                    BigInteger mul = new BigInteger(String.valueOf(w[i])).multiply(new BigInteger(String.valueOf(w[k]))).multiply(new BigInteger(String.valueOf(w[j])));
                    BigInteger val = dp[i][k].add(dp[k][j]).add(mul);
                    if ("0".equals(dp[i][j].toString())) dp[i][j] = val;
                    else if (val.compareTo(dp[i][j]) < 0) {
                        dp[i][j] = val;
                    }
                }
            }
        }
        return dp[0][n - 1].toString(10);
    }

    private static int dynamicProgramming0(int[] w) {
        int n = w.length;
        int[][] dp = new int[n][n];
        for (int length = 3; length <= n; length++) {
            for (int i = 0; i + length - 1 < n; i++) {
                int j = i + length - 1;
                dp[i][j] = INF;
                for (int k = i + 1; k < j; k++) {
                    dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + w[i] * w[k] * w[j]);
                }
            }
        }
        return dp[0][n - 1];
    }
}
